package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.Notice;
import com.gaogzhen.domain.WebConfig;
import com.gaogzhen.service.WebConfigService;
import com.gaogzhen.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description 资源配置管理器
 * @date 2022-06-03 10:35
 */
@RestController
@RequestMapping("/webConfigs")
@Api(tags = "资源配置")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;

    @GetMapping
    @ApiOperation("分页查询资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "name", value = "资源名称"),
            @ApiImplicitParam(name = "type", value = "资源类型")
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> findByPage(@ApiIgnore Page<WebConfig> page, String name, String type) {
        return R.ok(webConfigService.findByPage(page, name, type));
    }

    @PostMapping
    @ApiOperation("新增webConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "资源信息")
    })
    @PreAuthorize("hasAuthority('web_config_create')")
    public R add(@RequestBody @Validated WebConfig webConfig) {
        webConfigService.add(webConfig);
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("更新资源管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "资源信息")
    })
    @PreAuthorize("hasAuthority('web_config_update')")
    public R update(@RequestBody @Validated WebConfig webConfig) {
        webConfigService.updateWebConfig(webConfig);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation("删除资源管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "资源id集合")
    })
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R<Page<Notice>> delete(@RequestBody String []ids) {
        webConfigService.delete(ids);
        return R.ok();
    }

    @GetMapping("/banners")
    @ApiOperation("查询PC端banners")
    public R<List<WebConfig>> banners() {
        return R.ok(webConfigService.getPcBanners());
    }
}
